<template>
    <div>
        <div id="qq">
            <div id="aa">
                <span class="fa fa-angle-left" @touchstart="back"></span>
            </div>
            <div id="zz">上传商品</div>
            <div id="ww">···</div>
        </div>
        
        <label>商品名称:</label>
        <input type="text" v-model="goodInfo.name">
        <br>
        <label>商品价格:</label>
        <input type="text" v-model.number="goodInfo.price">

        <hr>

        <div v-for="(t,i) in goodInfo.type">
            <label>分类名称</label>
            <input type="text" v-model="t.name">
            <br>
            <label>特色亮点</label>
            <input type="text" placeholder="多个内容之间以空格隔开" @input="kindEdit(t,$event)">
            <button @click="delType(i)">X</button>
            <br>
            <br>
        </div>

        <button @touchstart="addType">添加分类</button>

        <hr>

        <div 
            v-for = "p in picUrl"
            class="pic-item" 
            :style="{'background-image':'url('+p+')'}"
        >

        </div>

        <label for="pic" id="pic-label">+</label>
        <input type="file" id="pic" multiple accept="image/*" @change="chooseImg">
        
        <hr>

        <button @touchstart="commit">提交</button>

    </div>
</template>

<script>


export default {
    data:function(){
        return {
            goodInfo:{
                name:"",
                price:0,
                type:[]
            },
            pics:[]
        }
    },
    methods:{
        back: function () {
            history.back();
        },
        addType:function(){
            var o = {
                name:"",
                kind:[]
            }
            this.goodInfo.type.push(o)
        },
        delType:function(i){
            this.goodInfo.type.splice(i,1);
        },
        kindEdit:function(t,e){
            var val = e.target.value;
            t.kind = val.split(" ");
            t.kind = t.kind.filter(function(e){
                return e;
            });
        },
        chooseImg:function(e){
            for(var i = 0;i<e.target.files.length;i++){
                this.pics.push(e.target.files[i]);
            }
        },
        commit:function(){
            var data = new FormData();
            var info = JSON.stringify(this.goodInfo);
            data.append("info",info);
            this.pics.forEach(function(e) {
                data.append("pic",e);
                // console.dir(data)
            }, this);
            // console.log(data)
            this.$store.dispatch("requestShangchuan", data)
                .then(res => {
                    this.layer.alert(res.data.msg);
                });
        }
    },
    computed:{
        picUrl:function(){
            return this.pics.map(function(e){
                return URL.createObjectURL(e);
            });
        }
    }
}
</script>

<style scoped>
    #qq {
        display: flex;
        height: 10vh;
        width: 100vw;
        border-bottom: solid 2px #bbb;
    }

    #aa {
        width: 10vh;
        height: 10vh;
        font-size: 8vh;
        text-align: center;
        line-height: 10vh;
        color: #999;
    }

    #zz {
        width: 50vh;
        text-align: center;
        line-height: 10vh;
    }

    #ww {
        width: 10vh;
        height: 10vh;
        font-size: 10vh;
        text-align: center;
        line-height: 10vh;
        color: #999;
    }
    #pic{
        display: none;
    }
    #pic-label{
        display: inline-block;
        width: 100px;
        height: 100px;
        border: solid 1px black;
        font-size: 80px;
        text-align: center;
    }
    .pic-item{
        display: inline-block;
        width: 100px;
        height: 100px;
        border: solid 1px black;
        background-size: 100% 100%;
        vertical-align: top;
        margin-left: 10px;
    }
</style>